import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import spring_annotation.config.MainProfileConfig;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.*;

public class IOCTest_Profile {

    AnnotationConfigApplicationContext aContext = new AnnotationConfigApplicationContext(MainProfileConfig.class);
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

    @Test
    public void test01() {
        String[] beanNamesForType = aContext.getBeanNamesForType(DataSource.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
        DataSource testDatasource = (DataSource) aContext.getBean("testDataSource");
        System.out.println(testDatasource);
//        Object devDataSource = aContext.getBean("devDataSource");
//        System.out.println(devDataSource);
//        Object prodDataSource = aContext.getBean("prodDataSource");
//        System.out.println(prodDataSource);
        aContext.close();
    }

    @Test
    public void test02() {
        ac.getEnvironment().setActiveProfiles("test", "dev");
        ac.register(MainProfileConfig.class);
        ac.refresh();
        String[] beanNamesForType = ac.getBeanNamesForType(DataSource.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
    }

    @Test
    public void test03() throws SQLException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ac.getEnvironment().setActiveProfiles("test");
        ac.register(MainProfileConfig.class);
        ac.refresh();
        String[] nameList = new String[]{"张三丰", "张无忌", "任我行", "令狐冲"};
        for (String s : nameList) {
            System.out.println(s);
        }
        DataSource testDatasource = (DataSource) ac.getBean("testDataSource");
        System.out.println(testDatasource);
        Connection conn = testDatasource.getConnection();

        ac.close();
    }

}
